package com.wuxiaofan.api;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.wuxiaofan.feign.UserFeignClient;
import com.wuxiaofan.vo.ReturnResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@Slf4j
public class TestApi {


    @Value("${wechat.appId}")
    private String wechatAppId;

    @SentinelResource(value = "test1")
    @RequestMapping("test1")
    public ReturnResult test1(){
        return ReturnResult.success("测试成功"+wechatAppId);
    }

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private UserFeignClient userFeignClient;

    @RequestMapping("test2")
    public ReturnResult test2(){
        List<ServiceInstance> serviceInstances = this.discoveryClient.getInstances("spring-cloud-alibaba-user");
        log.info("serviceInstances:{}",serviceInstances);
        return ReturnResult.success("测试成功",serviceInstances);
    }


    @RequestMapping("test3")
    public ReturnResult test3(){
        List<ServiceInstance> serviceInstances = this.discoveryClient.getInstances("spring-cloud-alibaba-user");
        log.info("serviceInstances:{}",serviceInstances);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<ReturnResult> returnResultResponseEntity = restTemplate.getForEntity(serviceInstances.get(0).getUri()+"test1",ReturnResult.class);
        return returnResultResponseEntity.getBody();
    }

    @RequestMapping("test4")
    public ReturnResult test4(){
        ReturnResult returnResult = userFeignClient.test1();
        return returnResult;
    }




}
